package com.cl.edu.web.teacher;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.util.Num62;
import com.cl.common.util.StringUtil;
import com.cl.common.util.UploadUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Teacher;
import com.cl.edu.entityVO.Photo;
import com.cl.edu.service.teacher.TeacherManager;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 教员证件操作
 * 
 * @author Administrator
 * 
 */
@Results({ @Result(name = "success", location = "tc_cardmanage.jsp") })
public class CardAction extends ActionSupport {

	@Autowired
	private TeacherManager teacherManager;
	
	private Teacher teacher; 
	private Long id;
	
	private File card; // 上传的文件
	private String cardFileName; // 上传的文件名称
	private String cardContentType; // 上传的文件类型
	private String cardName; // 用户自定义的照片名

	private String savePath = File.separator+"upload"+File.separator+"cards";

	private List<Photo> cardsList = new ArrayList<Photo>();		//证件列表
	/**
	 * 进入页面时查询已上传证件列表
	 */
	public String execute() throws Exception {
		teacherManager.evict(teacher);
		teacher = teacherManager.getTeacher(id);
		String cards = teacher.getCards();
		if(StringUtil.isNotEmpty(cards)){
			String[] urls = cards.split("@@");
			for(String url:urls){
				String[] temp = url.split("@");
				Photo photo = new Photo();
				photo.setName(temp[0]);
				photo.setDesc(temp[1]);
				cardsList.add(photo);
			}
		}
		
		return "success";
	}

	/**
	 * 上传证件
	 * @return
	 * @throws Exception
	 */
	public String upload() throws Exception {
		// 获取扩展名
		String ext = UploadUtils.getExtension(cardFileName);
		if (!"gif".equals(ext) && !"jpg".equals(ext) && !"png".equals(ext)) {
			addActionMessage("照片扩展名不符合要求!");
			return execute();
		}
		long size = card.length();
		if (size > 1024 * 1024 * 512) {
			addActionMessage("照片大小不能大于512K!");
			return execute();
		}
		// 重命名文件
		String newFileName = RandomStringUtils.random(8, Num62.N36_CHARS) + "." + ext;
		String fullPath = getSavePath() + File.separator+id+File.separator+newFileName;
		String parentFolder = UploadUtils.getParentFolder(fullPath);
		try {
			//存入物理文件
			File file = new File(parentFolder);
			UploadUtils.checkDirAndCreate(file);
			FileOutputStream fos = new FileOutputStream(fullPath);
			FileInputStream fis = new FileInputStream(getCard());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			
			//存入数据库
			teacher = teacherManager.getTeacher(id);
			String cards = teacher.getCards();
			if(StringUtil.isEmpty(cards)){
				cards = "";
			}
			cards+=newFileName+"@"+cardName+"@@";
			teacher.setCards(cards);
			teacherManager.updateTeacher(teacher);
			addActionMessage("上传成功!");
			
		} catch (Exception e) {
			addActionMessage("上传失败!");
			e.printStackTrace();
		}
		return execute();
	}
	
	
	/**
	 * 删除证件
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		teacher = teacherManager.getTeacher(id);
		String name = Struts2Utils.getParameter("name");
		File file = new File(getSavePath() + File.separator+id+File.separator+name);
		if(file.exists()){ //如果存在则删除文件
			file.delete();
		}
		String  cards = teacher.getCards();
		teacher.setCards(cards.replaceAll(name+"@.*@@", ""));
		teacherManager.updateTeacher(teacher);
		addActionMessage("删除成功!");
		return execute();
	}


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}


	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public File getCard() {
		return card;
	}

	public void setCard(File card) {
		this.card = card;
	}

	public String getCardFileName() {
		return cardFileName;
	}

	public void setCardFileName(String cardFileName) {
		this.cardFileName = cardFileName;
	}

	public String getCardContentType() {
		return cardContentType;
	}

	public void setCardContentType(String cardContentType) {
		this.cardContentType = cardContentType;
	}

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}

	public List<Photo> getCardsList() {
		return cardsList;
	}

	public void setCardsList(List<Photo> cardsList) {
		this.cardsList = cardsList;
	}

}
